package io.renren.modules.erp.controller;

import com.alibaba.fastjson.JSONObject;
import io.renren.common.constants.BusinessConstants;
import io.renren.common.constants.ExceptionConstants;
import io.renren.common.exception.BusinessRunTimeException;
import io.renren.common.utils.Constants;
import io.renren.common.utils.QueryUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.StringUtil;
import io.renren.modules.erp.entity.Unit;
import io.renren.modules.erp.service.UnitService;
import io.renren.modules.sys.controller.AbstractController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Description
 *
 * @Author: qiankunpingtai
 * @Date: 2019/4/1 15:38
 */
@RestController
@RequestMapping(value = "/unit")
public class UnitController extends AbstractController {
    private Logger logger = LoggerFactory.getLogger(UnitController.class);
    @Resource
    private UnitService unitService;
    @GetMapping(value = "/list")
    public R getUnitList(@RequestParam Map<String, String> params)throws Exception {
        String search = params.get(Constants.SEARCH);
        String name = StringUtil.getInfo(search, "name");
        String order = QueryUtils.order(params);
        int pageSize = Integer.valueOf(params.get( "pageSize"));
        int currPage = Integer.valueOf(params.get( "currentPage"));
        return R.ok().put("data",unitService.select(name, currPage, pageSize));
    }
    /**
     * create by: qiankunpingtai
     * website：https://qiankunpingtai.cn
     * description:
     *  批量删除系统配置信息
     * create time: 2019/3/29 11:15
     * @Param: ids
     * @return java.lang.Object
     */
    @RequestMapping(value = "/batchDeleteUnitByIds")
    public R batchDeleteUnitByIds(@RequestParam("ids") String ids,@RequestParam(value="deleteType",
            required =false,defaultValue= BusinessConstants.DELETE_TYPE_NORMAL)String deleteType) throws Exception {
        int i=0;
        if(BusinessConstants.DELETE_TYPE_NORMAL.equals(deleteType)){
            i= unitService.batchDeleteUnitByIdsNormal(ids,getUserId());
        }else if(BusinessConstants.DELETE_TYPE_FORCE.equals(deleteType)){
            i= unitService.batchDeleteUnitByIds(ids,getUserId());
        }else{
            logger.error("异常码[{}],异常提示[{}],参数,ids[{}],deleteType[{}]",
                    ExceptionConstants.DELETE_REFUSED_CODE,ExceptionConstants.DELETE_REFUSED_MSG,ids,deleteType);
            throw new BusinessRunTimeException(ExceptionConstants.DELETE_REFUSED_CODE,
                    ExceptionConstants.DELETE_REFUSED_MSG);
        }
        if(i<1){
            logger.error("异常码[{}],异常提示[{}],参数,ids[{}]",
                    ExceptionConstants.UNIT_DELETE_FAILED_CODE,ExceptionConstants.UNIT_DELETE_FAILED_MSG,ids);
            throw new BusinessRunTimeException(ExceptionConstants.UNIT_DELETE_FAILED_CODE,
                    ExceptionConstants.UNIT_DELETE_FAILED_MSG);
        }
        return R.ok();
    }

    @PostMapping(value = "/add")
    public R insert(@RequestBody Unit unit, HttpServletRequest request)throws Exception {
        if(1==unitService.insertUnit(unit, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PostMapping(value = "/update")
    public R update(@RequestBody String beanJson,@RequestParam("id") Long id, HttpServletRequest request)throws Exception {
        if(1==unitService.updateUnit(beanJson, id, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping(value = "/delete")
    public  R delete(@RequestParam("id")Long id, HttpServletRequest request)throws Exception {
        if(1==unitService.deleteUnit(id, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping(value = "/batchDelete")
    public R batchDelete(@RequestParam("ids")String ids, HttpServletRequest request)throws Exception {
        if(1==unitService.batchDeleteUnit(ids, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping(value = "/checkIsNameExist")
    public R checkIsNameExist(@RequestParam("id") Long id, @RequestParam("name")String name)throws Exception {
        if(0<unitService.checkIsNameExist(id, name)){
            return R.ok();
        }else{
            return R.error();
        }
    }
}
